import re
import uuid, json, hashlib
from WeChatMessage import work


def encry(password):
    md = hashlib.md5(password.encode())
    return md.hexdigest()


def main():
    uid = str(uuid.uuid1()).split('-')[-1]
    pattern = re.compile('\d+')
    uid = re.search(pattern, uid).group()
    key = int(uid) * 38 + 85 - 23
    result = encry(str(key))
    file = open('settings/config.json', 'r', encoding='utf-8')
    data = json.loads(file.read())
    if data['key'] == result:
        work()
    else:
        print(f'您的设备码为：{uid}')
        pwd = input('输入激活码：')
        if pwd == result:
            config = {
                "nick": data['nick'],
                "content": data['content'],
                "mode": data['mode'],
                "hour": data['hour'],
                "minutes": data['minutes'],
                "key": result,
                "workday": data['workday'],
                "unworkday": data['unworkday']
            }
            with open('settings/config.json', 'w', encoding='utf-8') as f:
                json.dump(config, f, ensure_ascii=False, indent=4)
            work()
        else:
            print('\n激活码输入有误！')
            main()


if __name__ == '__main__':
    main()
